package com.iplant.wms.controller.batch;

import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.SMSSService;
import com.iplant.wms.service.po.batch.MSSBatchRange;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/MSSBatchRange")
public class MSSBatchRangeController extends BasicController<MSSBatchRange> {

	public MSSBatchRangeController() {
		super(MSSBatchRange.class);
	}

	@Autowired
	SMSSService mSMSSService;
	@GetMapping("/RangeAll")
	Object RangeAll(HttpServletRequest request) {
		Object wResult = new Object();
		try {

			BMSEmployee wLoginUser = GetSession(request);
			int wCustomerID = StringUtils.parseInt(request.getParameter("CustomerID"), -1);
			int wMaterialID = StringUtils.parseInt(request.getParameter("MaterialID"), -1);
			String wMaterialGroupFullName = StringUtils.parseString(request.getParameter("MaterialGroupFullName"));
			Pagination wPagination = GetPagination(request);

			ServiceResult<List<MSSBatchRange>> wServiceResult = mSMSSService.MSS_QueryBatchRangeList(
					wLoginUser, wCustomerID, wMaterialGroupFullName, wMaterialID, wPagination);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			log.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@GetMapping("/RangeInfo")
	Object RangeInfo(HttpServletRequest request) {
		Object wResult = new Object();
		try {

			BMSEmployee wLoginUser = GetSession(request);
			int wCustomerID = StringUtils.parseInt(request.getParameter("CustomerID"));
			String wMaterialNo = StringUtils.parseString(request.getParameter("MaterialNo"));


			ServiceResult<MSSBatchRange> wServiceResult = mSMSSService.MSS_QueryBatchRange(wLoginUser, wCustomerID, wMaterialNo);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			log.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}


}
